import { createSelector } from 'reselect'
import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../constants/TodoFilters'

const getVisibilityFilter = state => state.visibilityFilter
const getTodos = state => state.todos

export const getVisibleTodos = createSelector (
    [getVisibilityFilter, getTodos],
    (visibilityFilter, todos) => {
        switch (visibilityFilter) {
            case SHOW_ALL:
                return todos
            case SHOW_COMPLETED:
                return todos.filter(t => t.completed)
            case SHOW_ACTIVE:
                return todos.filter(t => !t.completed)
            default:
                throw new Error ('不支持的过滤条件：'+ visibilityFilter)
        }
    }
)